
/****************************************************************************
**
** Copyright (C) 2004-2008 VATATA.com . All rights reserved.
**
** This file is the part of the Miniutil Poject.
** Vatata's miniutil library is lightweight and portable (for linux
** and Windows). The C++ Class Library included Threadpool, Dll danymic loading, 
** General socket application model and tcp server, httpserver/servlet, etc. 
** It is a pratical utility disigned for who don't want learn the Big C++ 
** Developing Platform, such as  Boost or ACE.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by VATATA.com.
** (or its successors, if any) and the Miniutil Project Aministrators. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://www.gnu.org/licenses/lgpl.html. If you are unsure which 
** license is appropriate for your use, please review the following 
** information: http://code.google.com/p/miniutil/ or contact 
** http://www.vatata.com.
**
** In addition, as a special exception, VATATA.com, as the sole
** copyright holder for Miniutil Project, grants users of VATATA
** P2P Platform the right to release and package with the related 
** libraries without the source code.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. vatata reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/


#include "miniutil/buffer.h"
#include "minitest/testcase.h"
#include "miniutil/buffer_alloc.h"

class buffercase_1:public testcase
{
public:
	buffercase_1(){}

	virtual void test()
	{
		char buf[33];
		for(int i=0; i< sizeof(buf); i++)
		{
			buf[i] = 'a'+i%26;
		}

		miniutil::data::buffer mbuf_1(buf, sizeof(buf));

		for(int i=0; i< mbuf_1.length(); i++)
		{
			std::cout<<mbuf_1[i]<<" ";
		}
		std::cout<<std::endl;

		int newlen = mbuf_1.enlarge(133);
		std::cout<<"enlarge to:"<<newlen<<" size:"<<mbuf_1.size()<<std::endl;

		for(int i=0; i< mbuf_1.length(); i++)
		{
			std::cout<<mbuf_1[i]<<" ";
		}

		newlen = mbuf_1.enlarge(4096);
		*mbuf_1.tail() = 'Z';
		std::cout<<std::endl;
	}
};


miniutil::data::pool_buffer_alloc alloc;

class buffercase_2:public testcase
{
public:
	buffercase_2(){}

	virtual void test()
	{
		miniutil::data::pool_buffer_alloc* palloc = miniutil::data::pool_buffer_alloc::getdefaultinstance();
		//miniutil::data::buffer::set_buffer_alloc(&alloc);

		miniutil::data::buffer::set_buffer_alloc(palloc);

		char buf[33];
		for(int i=0; i< sizeof(buf); i++)
		{
			buf[i] = 'a'+i%26;
		}

		miniutil::data::buffer mbuf_1(buf, sizeof(buf));

		for(int i=0; i< mbuf_1.length(); i++)
		{
			std::cout<<mbuf_1[i]<<" ";
		}
		std::cout<<std::endl;

		int newlen = mbuf_1.enlarge(133);
		std::cout<<"enlarge to:"<<newlen<<" size:"<<mbuf_1.size()<<std::endl;

		std::cout<<palloc->dump_stus()<<std::endl;

		for(int i=0; i< mbuf_1.length(); i++)
		{
			std::cout<<mbuf_1[i]<<" ";
		}

		newlen = mbuf_1.enlarge(4096);
		*mbuf_1.tail() = 'Z';

		std::cout<<palloc->dump_stus()<<std::endl;

		miniutil::data::buffer mbuf_2(1024);
		std::cout<<std::endl;
	
		palloc->setoverflowthreshold(1024);
		std::cout<<"have overflow?"<<palloc->wouldoverflow()<<std::endl;

		mbuf_1.resize(10,true);
		std::cout<<palloc->dump_stus()<<std::endl;
		//miniutil::data::buffer::set_buffer_alloc(NULL);
		//std::cout<<"dump status---:"<<std::endl;
		//std::cout<<alloc.dump_stus()<<std::endl;
		std::cout<<std::endl;
	}
};

testcase *t_buffer_1 = new buffercase_1();
testcase *t_buffer_2 = new buffercase_2();